
package data;

import java.sql.Date;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;



public class Article
{
	
	public int id = 0;//id从1开始数起
	public String title = null;
	public int user_id = 0;
	public String author = null;
	public Date upload_date = null;
	public String content = "";
	
	
	public Article(int id, String title, int user_id, String author, Date upload_date, String content)
	{
		this.id = id;
		this.title = title;
		this.user_id = user_id;
		this.author = author;
		this.upload_date = upload_date;
		this.content = content;
	}
	
	
	public String getJSONObjectString()
	{
		ObjectMapper mapper = new ObjectMapper(); 
		try
		{
			return mapper.writeValueAsString(this);
		}
		catch (JsonProcessingException e) {e.printStackTrace();}
		
		return null;
	}
	
	
	public JsonNode getJSONObject()
	{
		JsonNode node = null;
		try
		{
			node = new ObjectMapper().readTree(this.getJSONObjectString());
		}
		catch (JsonMappingException e) {e.printStackTrace();}
		catch (JsonProcessingException e) {e.printStackTrace();}
		return node;
	}
	
	
	public String getXMLNodeString()
	{
		StringBuilder sb = new StringBuilder("<article>\n");
		
		sb.append("\t<id>" + this.id + "</id>\n");
		sb.append("\t<title>" + this.title + "</title>\n");
		sb.append("\t<author>" + this.author + "</author>\n");
		sb.append("\t<date>" + this.upload_date + "</date>\n");
		sb.append("\t<content><![CDATA[\n" + this.content + "\n]]>\n\t</content>\n");
		sb.append("</article>\n\n");
		
		return sb.toString();
	}
}













